
package de.justphil.tcg.tcgandroidclient.network.websocket;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import de.justphil.tcg.tcgandroidclient.msgdata.Error;
import de.justphil.tcg.tcgandroidclient.msgdata.Info;
import de.justphil.tcg.tcgandroidclient.msgdata.Progress;
import de.justphil.tcg.tcgandroidclient.util.TCGLog;
import android.os.Message;

public class WebSocketMessageDecoder {
	
	public static Message decodeMessage(String msgString) {
	
		TCGLog.d(new Object[] {
				"Receiving message: ", msgString });
		Message msg;
		try {
			JSONObject msgJson = new JSONObject(msgString);
			String subject = msgJson.getString("subject");
			/* MSG_ERROR */
			if (subject.equals("err")) {
				JSONObject eJson = msgJson.getJSONObject("e");
				msg = Message.obtain(null, WebSocketService.MSG_ERROR);
				int code = eJson.getInt("code");
				String errorMsg = eJson.getString("msg");
				msg.obj = new Error(code, errorMsg);
			}
			else if (subject.equals("game-p")) {
				JSONObject pJson = msgJson.getJSONObject("p");
				if (pJson.has("cmd")) {
					/* MSG_RESUME_RQ */
					if (pJson.getString("cmd").equals("resume")) {
						msg = Message.obtain(null, WebSocketService.MSG_RESUME_RQ);
					}
					/* MSG_SELECT_RQ */
					else if (pJson.getString("cmd").equals("select")) {
						msg = Message.obtain(null, WebSocketService.MSG_SELECT_RQ);
						msg.arg1 = pJson.getInt("s");
					}
					/* MSG_WAIT_RQ */
					else {
						msg = Message.obtain(null, WebSocketService.MSG_WAIT_RQ);
						msg.arg1 = pJson.getInt("s");
					}
				}
				
				/* MSG_WINNER */
				else if (pJson.has("winner")) {
					msg = Message.obtain(null, WebSocketService.MSG_WINNER);
					msg.arg1 = pJson.getInt("winner");
				}
				/* MSG_PROGRESS */
				else {
					msg = Message.obtain(null, WebSocketService.MSG_PROGRESS);
					int slot = pJson.getInt("s");
					long playerId = pJson.getLong("pId");
					int cardType = pJson.getInt("cId");
					JSONArray propertiesJson = pJson.getJSONArray("pIds");
					int[] properties = new int[propertiesJson.length()];
					for (int i = 0; i < properties.length; i++) {
						properties[i] = propertiesJson.getInt(i);
					}
					int life;
					int attack;
					int defense;
					int agility;
					if (cardType != -1) {
						life = pJson.getInt("l");
						attack = pJson.getInt("at");
						defense = pJson.getInt("d");
						agility = pJson.getInt("ag");
					}
					else {
						life = attack = defense = agility = 0;
					}
					msg.obj =
							new Progress(slot, playerId, cardType, properties, life, attack,
									defense, agility);
				}
			}
			/* MSG_INFO */
			else if (subject.equals("game-i")) {
				msg = Message.obtain(null, WebSocketService.MSG_INFO);
				JSONObject iJson = msgJson.getJSONObject("i");
				msg.obj =
						new Info(iJson.getInt("t"), iJson.getString("key"), iJson.getString("msg"));
			}
			/* MSG_JOINED */
			else if (subject.equals("game-j")) {
				msg = Message.obtain(null, WebSocketService.MSG_JOINED);
				JSONObject jJson = msgJson.getJSONObject("j");
				msg.arg1 = jJson.getInt("code");
			}
			else {
				msg = Message.obtain(null, -1);
			}
		}
		catch (JSONException e) {
			TCGLog.e(new Object[] {
					"Error parsing Json Message: ", msgString, "\n" }, e);
			msg = Message.obtain(null, WebSocketService.MSG_DECODING_ERROR, msgString);
		}catch (Exception e) {
			TCGLog.e(new Object[] {
					"Unexpected Exception decoding Message: ", msgString, "\n" }, e);
			msg = Message.obtain(null, WebSocketService.MSG_DECODING_ERROR, msgString);
		}
		return msg;
	}
}
